package com.roger.homecenterscreen.ui.components

import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.roger.homecenterscreen.voice.VoiceState
import com.roger.homecenterscreen.voice.SynthesisState

/**
 * 语音状态底部栏
 * 显示在MainActivity最底部的音频交互状态区域
 */
@Composable
fun VoiceStatusBottomBar(
    voiceState: VoiceState,
    synthesisState: SynthesisState?,
    recognitionResult: String?,
    isVoiceInitialized: Boolean = false,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(
            containerColor = Color.Black.copy(alpha = 0.8f)
        ),
        shape = RoundedCornerShape(bottomStart = 12.dp, bottomEnd = 12.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            // 左侧：语音状态图标和动画
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                VoiceStateIcon(
                    voiceState = voiceState,
                    synthesisState = synthesisState,
                    isVoiceInitialized = isVoiceInitialized,
                    size = 24.dp
                )
                
                Text(
                    text = getVoiceStateText(voiceState, synthesisState, isVoiceInitialized),
                    color = Color.White,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Medium
                )
            }
            
            // 右侧：识别结果文本
            recognitionResult?.takeIf { it.isNotBlank() }?.let { result ->
                Row(
                    modifier = Modifier.weight(1f),
                    horizontalArrangement = Arrangement.End
                ) {
                    Text(
                        text = "\"$result\"",
                        color = Color.White.copy(alpha = 0.9f),
                        fontSize = 11.sp,
                        textAlign = TextAlign.End,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier.padding(start = 8.dp)
                    )
                }
            }
        }
    }
}

/**
 * 语音状态图标
 */
@Composable
private fun VoiceStateIcon(
    voiceState: VoiceState,
    synthesisState: SynthesisState?,
    isVoiceInitialized: Boolean,
    size: androidx.compose.ui.unit.Dp
) {
    val primaryColor = getPrimaryColor(voiceState, synthesisState, isVoiceInitialized)
    val shouldAnimate = shouldAnimate(voiceState, synthesisState, isVoiceInitialized)
    
    // 动画效果
    val animatedAlpha = if (shouldAnimate) {
        val infiniteTransition = rememberInfiniteTransition(label = "voice_animation")
        infiniteTransition.animateFloat(
            initialValue = 0.3f,
            targetValue = 1f,
            animationSpec = infiniteRepeatable(
                animation = tween(
                    durationMillis = 1000,
                    easing = LinearEasing
                ),
                repeatMode = RepeatMode.Reverse
            ),
            label = "alpha_animation"
        ).value
    } else {
        1f
    }
    
    Box(
        modifier = Modifier
            .size(size)
            .background(
                color = primaryColor.copy(alpha = animatedAlpha),
                shape = RoundedCornerShape(size / 2)
            ),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = getVoiceEmoji(voiceState, synthesisState, isVoiceInitialized),
            fontSize = (size.value * 0.6).sp,
            textAlign = TextAlign.Center
        )
    }
}

/**
 * 获取语音状态文本
 */
private fun getVoiceStateText(voiceState: VoiceState, synthesisState: SynthesisState?, isVoiceInitialized: Boolean): String {
    return when (voiceState) {
        VoiceState.IDLE -> if (isVoiceInitialized) "待机" else "未启动"
        VoiceState.LISTENING_WAKEUP -> "等待唤醒"
        VoiceState.WAKEUP_DETECTED -> "唤醒成功"
        VoiceState.LISTENING_SPEECH, VoiceState.RECORDING, VoiceState.ASR_START, VoiceState.RECOGNIZING -> "正在聆听"
        VoiceState.PROCESSING -> "AI思考中"
        VoiceState.SPEAKING -> when (synthesisState) {
            SynthesisState.IDLE -> "语音待机"
            SynthesisState.SYNTHESIZING -> "正在合成"
            SynthesisState.COMPLETED -> "回答完成"
            SynthesisState.ERROR -> "语音错误"
            null -> "正在说话"
        }
        VoiceState.ERROR -> "发生错误"
    }
}

/**
 * 获取语音状态图标
 */
private fun getVoiceEmoji(voiceState: VoiceState, synthesisState: SynthesisState?, isVoiceInitialized: Boolean): String {
    return when (voiceState) {
        VoiceState.IDLE -> if (isVoiceInitialized) "😴" else "🔇"
        VoiceState.LISTENING_WAKEUP -> "👂"
        VoiceState.WAKEUP_DETECTED -> "⚡"
        VoiceState.LISTENING_SPEECH, VoiceState.RECORDING, VoiceState.ASR_START, VoiceState.RECOGNIZING -> "🎤"
        VoiceState.PROCESSING -> "🤔"
        VoiceState.SPEAKING -> when (synthesisState) {
            SynthesisState.SYNTHESIZING -> "🤖"
            SynthesisState.COMPLETED -> "🗣️"
            else -> "💬"
        }
        VoiceState.ERROR -> "❌"
    }
}

/**
 * 获取主要颜色
 */
private fun getPrimaryColor(voiceState: VoiceState, synthesisState: SynthesisState?, isVoiceInitialized: Boolean): Color {
    return when (voiceState) {
        VoiceState.IDLE -> if (isVoiceInitialized) Color.Gray else Color(0xFF808080) // 深灰色表示未启动
        VoiceState.LISTENING_WAKEUP -> Color.Blue
        VoiceState.WAKEUP_DETECTED -> Color.Yellow
        VoiceState.LISTENING_SPEECH, VoiceState.RECORDING, VoiceState.ASR_START, VoiceState.RECOGNIZING -> Color.Green
        VoiceState.PROCESSING -> Color(0xFFFFA500) // Orange
        VoiceState.SPEAKING -> Color.Cyan
        VoiceState.ERROR -> Color.Red
    }
}

/**
 * 是否需要动画
 */
private fun shouldAnimate(voiceState: VoiceState, synthesisState: SynthesisState?, isVoiceInitialized: Boolean): Boolean {
    return when (voiceState) {
        VoiceState.IDLE -> false // 无论是否初始化，待机状态都不动画
        VoiceState.LISTENING_WAKEUP,
        VoiceState.WAKEUP_DETECTED,
        VoiceState.LISTENING_SPEECH,
        VoiceState.RECORDING,
        VoiceState.ASR_START,
        VoiceState.RECOGNIZING,
        VoiceState.PROCESSING -> true
        VoiceState.SPEAKING -> synthesisState == SynthesisState.SYNTHESIZING
        else -> false
    }
}